use excelwriter::*;
use rocket::{post, get, State, Request, Response};
use rocket::response::{self,content,NamedFile,Responder};
use uuid::Uuid;
use tokio::fs::File;
use std::{io, fmt, str};
// get函数hello
#[get("/export1.xlsx")]
pub async  fn export1() -> Option<NamedFile> {
    match std::fs::create_dir("/tmp") {
        Err(why) => println!("! {:?}", why.kind()),
        Ok(_) => {},
    }
    let filename =format!("/tmp/{}.xlsx",Uuid::new_v4() ) ;
    let mut wb = Workbook::create(&filename);
    let mut sheet = wb.create_sheet("第一页");

    // set column width
    sheet.add_column(Column { width: 30.0 });
    sheet.add_column(Column { width: 30.0 });
    sheet.add_column(Column { width: 80.0 });
    sheet.add_column(Column { width: 60.0 });

    wb.write_sheet(&mut sheet, |sheet_writer| {
        let sw = sheet_writer;
        sw.append_row(row!["Name", "Title","Success","XML Remark"])?;
        sw.append_row(row!["Amy", (), true,"<xml><tag>\"Hello\" & 'World'</tag></xml>"])?;
        sw.append_blank_rows(2);
        sw.append_row(row!["Tony", blank!(2), "retired"])
    }).expect("write excel error!");

    let mut sheet = wb.create_sheet("第二页");
    wb.write_sheet(&mut sheet, |sheet_writer| {
        let sw = sheet_writer;
        sw.append_row(row!["Name", "Title","Success","Remark"])?;
        sw.append_row(row!["Amy", "Manager", true])
    }).expect("write excel error!");

    
    wb.close().expect("close excel error!");

    NamedFile::open(&filename).await.ok()
}

#[get("/export2")]
pub async fn export2<'r>() -> Result<rocket::Response<'r>, rocket::http::Status> {
    match std::fs::create_dir("/tmp") {
        Err(why) => println!("! {:?}", why.kind()),
        Ok(_) => {},
    }
    let filename =format!("/tmp/{}.xlsx",Uuid::new_v4() ) ;
    let mut wb = Workbook::create(&filename);
    let mut sheet = wb.create_sheet("第一页");

    // set column width
    sheet.add_column(Column { width: 30.0 });
    sheet.add_column(Column { width: 30.0 });
    sheet.add_column(Column { width: 80.0 });
    sheet.add_column(Column { width: 60.0 });

    wb.write_sheet(&mut sheet, |sheet_writer| {
        let sw = sheet_writer;
        sw.append_row(row!["Name", "Title","Success","XML Remark"])?;
        sw.append_row(row!["Amy", (), true,"<xml><tag>\"Hello\" & 'World'</tag></xml>"])?;
        sw.append_blank_rows(2);
        sw.append_row(row!["Tony", blank!(2), "retired"])
    }).expect("write excel error!");

    let mut sheet = wb.create_sheet("第二页");
    wb.write_sheet(&mut sheet, |sheet_writer| {
        let sw = sheet_writer;
        sw.append_row(row!["Name", "Title","Success","Remark"])?;
        sw.append_row(row!["Amy", "Manager", true])
    }).expect("write excel error!");

    
    wb.close().expect("close excel error!");

    let response = Response::build()
         .raw_header("Content-Type","application/vnd.ms-excel;charset=utf-8")
         .raw_header("Content-Disposition", "attachment;filename=导出excel.xlsx")
         .chunked_body(File::open(&filename).await.unwrap(), 8096)
         .ok();
         
     response
}

